package com.codingfly.listener;

import com.alibaba.fastjson.JSON;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Service;

/**
 * messageModel=MessageModel.CLUSTERING
 * 监听模式，有消息就会消费
 */
@Service
@RocketMQMessageListener(nameServer="${rocketmqCluster1.name-server}", topic = "test2-topic",
                         consumerGroup = "other-consumer-group1", messageModel = MessageModel.CLUSTERING)
public class RocketMQConsumer1 implements RocketMQListener<MessageExt> {

    @Override
    public void onMessage(MessageExt msg) {
        System.out.println(JSON.toJSONString(msg));
        System.out.printf("RocketMQMessageListener other-consumer-group1 收到消息: 主题=%s, 标签=%s, 消息ID=%s, 内容=%s%n",
                msg.getTopic(), msg.getTags(), msg.getMsgId(), new String(msg.getBody()));
        RocketMQConsumer3.printCostTime(new String(msg.getBody()));
    }

}
